package com.hongyi.web.controller;

import com.hongyi.result.Result;
import com.hongyi.web.dto.SysPostDto;
import com.hongyi.web.mapper.SysPostMapper;
import com.hongyi.web.pojo.SysPost;
import com.hongyi.web.query.SysPostQueryDto;
import com.hongyi.web.query.SysUserQueryDto;
import com.hongyi.web.service.ISysPostService;
import com.hongyi.web.utils.PageResult;
import com.hongyi.web.utils.StringUtils;
import com.hongyi.web.vo.PostVo;
import com.hongyi.web.vo.SysPostVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.hongyi.result.Result.error;

/**
 * @Author hongyi
 * @Date 2025/6/16 10:32
 */

@RestController
@RequestMapping("/sysPost")
public class SysPostController {
    @Autowired
    private ISysPostService postService;

    @Autowired
    private SysPostMapper postMapper;

    /**
     * 获取岗位列表
     */
    @GetMapping("/selectPage")
    public Result selectPage(SysPostQueryDto postQueryDto,
                             @RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize)
    {
        PageResult<PostVo> list = postService.selectPage(postQueryDto,pageNum,pageSize);
        return Result.success(list);
    }


    /**
     * 根据岗位编号获取详细信息
     */
    @GetMapping(value = "/{postId}")
    public Result getPostById(@PathVariable Long postId)
    {
        PostVo list =postService.selectPostById(postId);
        return Result.success(list);
    }

    /**
     * 新增岗位
     */
    @PostMapping("/add")
    public Result add(@RequestBody SysPostDto sysPostDto) {
        SysPost sysPostCode = postMapper.checkPostCodeUnique(sysPostDto.getPostCode());
        SysPost sysPostName = postMapper.checkPostNameUnique(sysPostDto.getPostName());
        if (sysPostCode !=null && sysPostCode.getPostId() !=sysPostDto.getPostId()) {
            return error2("新增岗位'" + sysPostDto.getPostName() + "'失败，岗位名称已存在");
        } else if (!checkPostNameUnique(sysPostDto)) {
            return error2("岗位名称已存在");
        }
        postService.insertPost(sysPostDto);
        return Result.success("添加数据成功");
    }

    public boolean checkPostNameUnique(SysPostDto sysPostDto) {
        Long postId = StringUtils.isNull(sysPostDto.getPostId()) ? -1L : sysPostDto.getPostId();
        // postName
        SysPost postName = postMapper.checkPostNameUnique(sysPostDto.getPostName());
        if (StringUtils.isNotNull(postName) && postName.getPostId().longValue() != postId.longValue()) {
            return false;
        }
        return true;
    }


    public boolean checkPostCodeUnique(SysPostDto sysPostDto) {
        Long postId = StringUtils.isNull(sysPostDto.getPostId()) ? -1L : sysPostDto.getPostId();
        SysPost postCode = postMapper.checkPostCodeUnique(sysPostDto.getPostCode());
        if (StringUtils.isNotNull(postCode) && postCode.getPostId().longValue() !=postId.longValue()) {
            return false;
        }
        return true;
    }


    /**
     * 修改岗位
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody SysPostDto sysPostDto)
    {
//        SysPost sysPostCode = postMapper.checkPostCodeUnique(sysPostDto.getPostCode());
        SysPost sysPostName = postMapper.checkPostNameUnique(sysPostDto.getPostName());
        if (!checkPostCodeUnique(sysPostDto)) {
            return error2("修改岗位'" + sysPostDto.getPostName() + "'失败，岗位名称已存在");
        } else if (sysPostName !=null && sysPostName.getPostId()!=sysPostDto.getPostId()) {
            return error2("岗位名称已存在");
        }
        postService.updatePost(sysPostDto);

        return Result.success("修改数据成功");
    }

    public Result error2(String message) {
        return Result.error(message);
    }

    /**
     * 删除岗位
     */
    @DeleteMapping("/{postIds}")
    public Result remove(@PathVariable Long[] postIds)
    {
        postService.deletePostByIds(postIds);
        return Result.success("删除数据成功");
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public Result optionselect()
    {

        List<SysPost> posts = postService.selectPostAll();
        List<SysPostVo> postVos = new ArrayList<>();
        if (posts.size() > 0) {
            for (SysPost post : posts) {
                SysPostVo postVo = new SysPostVo();
                BeanUtils.copyProperties(post,postVo);
                postVos.add(postVo);
            }
        }
        return Result.success(postVos);
    }

}
